<template>
  <view class="container">
    <view class="book-info-layout">
      <image class="book-image" :src="oldBook.image"></image>
      <view class="book-info">
        <view class="book-name">{{ oldBook.bookName }}</view>
        <view class="book-info_content">{{ oldBook.year }}</view>
        <view class="book-info_content">{{ oldBook.author }}</view>
      </view>
    </view>
    <view class="book-desc">{{ oldBook.desc }}</view>
    <view class="divider-line"></view>
    <view v-for="(item,index) in oldBook.chapters" :key="index">
      <view class="book-chapters" @click="onChapterClick(item,index)">{{ item }}</view>
    </view>
  </view>
</template>

<script>
import {
  bookDetail
} from '@/router.js'

export default {
  data() {
    return {
      oldBook: {}
    }
  },
  onLoad(options) {
    var bookName = options.bookName
    uni.setNavigationBarTitle({
      title: bookName
    })
    this.getBookChapters(bookName)
  },
  methods: {
    // 书籍章节
    getBookChapters(bookName) {
      this.$tools.showLoading()
      uniCloud.callFunction({
        name: 'database',
        data: {
          collection: 'oldbook',
          type: 'chapter',
          bookName
        },
        success: (res) => {
          uni.hideLoading()
          console.log(res)
          let result = res.result.data
          if (result && result.length > 0) {
            var oldBook = result[0]
            this.oldBook = oldBook
          }
        }
      })
    },
    //章节点击监听
    onChapterClick(item, index) {
      let bookName = this.oldBook.bookName
      console.log(bookName)
      uni.navigateTo({
        url: bookDetail + `?bookName=${bookName}&chapterName=${item}&chapterIndex=${index}`
      })
    }
  }
}
</script>

<style>
view {
  font-family: PingFangSC-Regular;
  font-weight: 400;
}

text {
  display: flex;
  font-family: PingFangSC-Regular;
  font-weight: 500;
}

.container {
  width: calc(100% - 15px);
  margin: 15px;
}

.book-name {
  font-size: x-large;
  font-weight: 600;
  margin-bottom: 10px;
}

.book-info-layout {
  display: flex;
  flex-direction: row;
}

.book-image {
  width: 100px;
  height: 142px;
}

.book-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.book-info_content {
  margin-left: 15px;
}

.book-desc {
  font-size: 14px;
  color: #666;
  margin-top: 10px;
  margin-right: 20px;
}

.divider-line {
  margin: 10px 0px;
  width: 100%;
  height: 1px;
  background: #999;
}

.book-chapters {
  padding: 5px;
  color: #555;
  font-size: 14px;
  border-bottom: 1px solid #eeeeee;
}
</style>
